package com.iman.sumer.web.teamgroup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.iman.common.support.Page;
import com.iman.common.util.JsonMapper;
import com.iman.sumer.domain.User;
import com.iman.sumer.repository.IRedisGo;
import com.iman.sumer.service.UserService;
import com.iman.sumer.service.teamgroup.GroupActivityService;
import com.iman.sumer.service.teamgroup.GroupDynamicService;
import com.iman.sumer.service.teamgroup.GroupMemberService;
import com.iman.sumer.service.teamgroup.GroupShareService;
import com.iman.sumer.service.teamgroup.GroupTopicService;
import com.iman.sumer.service.teamgroup.TeamGroupService;
import com.iman.sumer.util.LinkMessages;
import com.iman.sumer.vo.teamgroup.GroupActivityVo;
import com.iman.sumer.vo.teamgroup.GroupMemberVo;
import com.iman.sumer.vo.teamgroup.GroupShareVo;
import com.iman.sumer.vo.teamgroup.GroupTopicVo;
import com.iman.sumer.vo.teamgroup.SumerGroupExtDTO;
import com.iman.sumer.vo.teamgroup.TeamGroupVo;
import com.iman.sumer.web.BaseController;

 
/**
 *  群组
 */
@Controller
@RequestMapping(value="/teamgroup/*")
public class TeamGroupController extends BaseController{
	//日志
	private static final Logger logger = LoggerFactory
			.getLogger(TeamGroupController.class);
	
	//群组
	@Autowired
	private TeamGroupService teamGroupService;
 
	//活动
	@Autowired
	private GroupActivityService groupActivityService;

	//共享
	@Autowired
	private GroupShareService groupShareService;

	//话题
	@Autowired
	private GroupTopicService groupTopicService;

	//成员
	@Autowired
	private GroupMemberService groupMemberService;

	//群组动态
	@Autowired
	private GroupDynamicService groupDynamicService;
	
	//用户
	@Autowired
	private  UserService   userService;
	
	//隊列
	@Autowired
	private IRedisGo redisGo;
	/**
	 * 首页－》家园－》群组
	 * 
	 * 家园群组首页
	 * 
	 */
	@RequestMapping(value="/query_homeGroup_main")
	public ModelAndView queryHomeGroupMain(HttpServletRequest request){	
		logger.info("[家园群组]页面加载开始...");
		Map<String,Object> model = new HashMap<String,Object>();
		//model.put("user", "测试传递变量" );
		//获取登陆用户ID
		Long teacherid = this.getUserIdInSession(request);
		
		model.put("pageSize", Page.DEFAULT_PAGE_SIZE);
		//跳转群组主页
		ModelAndView view = new ModelAndView("teamgroup/homeGroup_main",model);
		
		//每个做后都要加上这个
		putResourceSrc(view ,request );

		logger.info("[家园群组]页面加载返回...");
		return view;
	}
	
	
	/**
	 * 首页－》家园－》群组－》热门群组 
	 *  @param request
	 *  @return
	 */
	@RequestMapping(value="/query_homeGroup_hot/{pageNo}/{pageSize}")
	public ModelAndView hotGroupList(HttpServletRequest request,
			@Valid @PathVariable  String pageNo, 
			@Valid @PathVariable String pageSize){	
		logger.info("[热门群组]获取热门群组开始...");
		Map<String,Object> model = new HashMap<String,Object>();
		//model.put("user", "测试传递变量" );
		Long user_id = this.getUserIdInSession(request);
		
        Map params = new HashMap();
		//每页条数
		if(null==pageSize||"".equals(pageSize)){
			pageSize = Page.DEFAULT_PAGE_SIZE+"";
		}
		
		if(null==pageNo||"".equals(pageNo)){
			pageNo = "1";
		}
		params.put("ps", pageSize);
		params.put("pn", pageNo);
		params.put("member_id", user_id);
		
		//获取热门群组分页列表
		logger.info("[热门群组]获取热门群组列表信息...");
		Page<TeamGroupVo> data = teamGroupService.getHotGroupList(params);
		model.put("data", data);
		//总页数
		model.put("sumPageNum", data.getTotalPageCount());
		model.put("pageSize", Page.DEFAULT_PAGE_SIZE);
		model.put("pageNo",pageNo);
		model.put("user_id", user_id);
		model.put("showImgURL", LinkMessages.getString("SHOW_IMG_URL"));
		
		ModelAndView view = new ModelAndView("teamgroup/homeGroup_hot",model);
				
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		//点击tab颜色
		//view.addObject("tabstatus" , "2");
		logger.info("[热门群组]获取热门群组结束...");
				
		return view;
	}
	
	
/******************************************************************************************************************************/
	
	 
	
	/**
	 *  首页－》家园－》群组－》群组活动
	 *  @return
	 */
	@RequestMapping(value="/query_homeGroup_activity/{pageNo}/{pageSize}")
	public ModelAndView groupActivityList(HttpServletRequest request,
			@Valid @PathVariable  String pageNo, 
			@Valid @PathVariable String pageSize){	
		logger.info("[群组活动]获取群组活动开始...");
		Map<String,Object> model = new HashMap<String,Object>();
		//model.put("user", "测试传递变量" );
		Long user_id = this.getUserIdInSession(request);
		
        Map params = new HashMap();
		
		if(null==pageSize||"".equals(pageSize)){
//			pageSize = Page.DEFAULT_PAGE_SIZE+"";
			pageSize = 1+"";
		}
		if(null==pageNo||"".equals(pageNo)){
			pageNo = "1";
		}
		params.put("ps", pageSize);
		params.put("pn", pageNo);
		params.put("member_id", user_id);
		logger.info("[群组活动]获取分页群组活动列表...");
		Page<GroupActivityVo> data = groupActivityService.getActivityList(params);
		model.put("data", data);
		//总页数
		model.put("sumPageNum", data.getTotalPageCount());
		model.put("user_id", user_id);
		
		ModelAndView view = new ModelAndView("teamgroup/homeGroup_activity",model);
				
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		//点击tab颜色
		//view.addObject("tabstatus" , "2");

		logger.info("[群组活动]获取群组活动结束...");
		return view;
	}
	
	/**
	 *  具体群组的  群组活动
	 *  首页－》家园－》群组－》点击选择单个群组进入群组详细信息－》群组活动
	 *  @return
	 */
	
	@RequestMapping(value="/query_group_activity/{groupId}/{pageNo}/{pageSize}")
	public ModelAndView groupGroupActivityList(HttpServletRequest request,
			@Valid @PathVariable String groupId,
			@Valid @PathVariable  String pageNo, 
			@Valid @PathVariable String pageSize,String activityId){	
		logger.info("[群组活动]获取群组活动开始...");
		Map<String,Object> model = new HashMap<String,Object>();
		//model.put("user", "测试传递变量" );
		Long user_id = this.getUserIdInSession(request);
        Map params = new HashMap();
		params.put("group_id", groupId);
		params.put("member_id", user_id);
		
		
		if(null==pageSize||"null".equals(pageSize)){
			pageSize = Page.DEFAULT_PAGE_SIZE+"";
		}else if("0".equals(pageSize)){
			pageSize = Page.DEFAULT_PAGE_SIZE+"";
		}
		
		if(null==pageNo||"".equals(pageNo)){
			pageNo = "1";
		}
		params.put("ps", pageSize);
		params.put("pn", pageNo);
		
		logger.info("[群组活动]获取群组活动列表...");
		Page<GroupActivityVo> data = groupActivityService.getActivityList(params);
		int isMember = groupMemberService.isGroupMember(params);
		model.put("data", data);
		//总页数
		model.put("sumPageNum", data.getTotalPageCount());
		model.put("pageSize", Page.DEFAULT_PAGE_SIZE);
		model.put("pageNo",pageNo);
		model.put("group_id", groupId);
		model.put("user_id", user_id);
		model.put("activityId", activityId);
		model.put("isMember", isMember);
		
		//总页数
		ModelAndView view = new ModelAndView("teamgroup/group_activity",model);
				
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		
		logger.info("[群组活动]获取群组活动结束...");
		return view;
	}
	

	
	/**
	 * 家园-群组-群组话题
	 * 首页－》家园－》群组－》群组话题
	 * @return
	 */
	@RequestMapping(value="/query_homeGroup_topic/{pageNo}/{pageSize}")
	@ResponseBody
	public ModelAndView groupTopicList(HttpServletRequest request,
			@Valid @PathVariable  String pageNo, 
			@Valid @PathVariable String pageSize){
		 logger.info("[群组话题]获取群组话题开始...");
		
		 Long user_id = this.getUserIdInSession(request);
			
	        Map params = new HashMap();
	        Map model = new HashMap();
			if(null==pageSize||"".equals(pageSize)){
				pageSize = 1+"";
			}
			if(null==pageNo||"".equals(pageNo)){
				pageNo = "1";
			}
			params.put("ps", pageSize);
			params.put("pn", pageNo);
			params.put("member_id", user_id);
			logger.info("[群组话题]获取群组话题列表...");
			Page<GroupTopicVo> data = groupTopicService.getGroupTopicList(params);
			model.put("data", data);
			//总页数
			model.put("sumPageNum", data.getTotalPageCount());
			model.put("user_id", user_id);
			
			ModelAndView view = new ModelAndView("teamgroup/homeGroup_topic",model);
			Map<String,Object> result = new HashMap<String,Object>();
			result.put("success", true);
			logger.info("[群组话题]获取群组话题结束...");
			return view;

	}
	
	
	/**
	 * 点击群组 进入本群组详细信息
	 * 首页－》家园－》群组－》点击选择单个群组进入群组详细信息－》主页
	 * @return
	 */
	@RequestMapping(value="/query_group_main/{group_id}")
	@ResponseBody
	public ModelAndView getGroupSingle(HttpServletRequest request,@Valid @PathVariable  long group_id,String flag,String topicId,String activityId){
		logger.info("[群组主页]获取群组主页开始...");
		System.out.println(flag+"==========================="+group_id);
		Long user_id = this.getUserIdInSession(request);
		Map model = new HashMap();
		
		List<Map> map = teamGroupService.getGroupByID(group_id);
		
		model.put("data", map.get(0));
		model.put("group_id", group_id);
		model.put("user_id", user_id);
		model.put("showImgURL", LinkMessages.getString("SHOW_IMG_URL"));
		model.put("pageSize", Page.DEFAULT_PAGE_SIZE);
		if(null==flag || "".equals(flag)){
			flag = "main";
		}
		model.put("flag", flag);
		
		model.put("topicId", topicId);
		model.put("activityId", activityId);
		
		ModelAndView view = new ModelAndView("teamgroup/group_main",model);
		
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		logger.info("[群组主页]获取群组主页结束...");
		
		return view;
	}
	
	
	/**
	 * 
	 * 群组主页
	 * 首页－》家园－》群组－》点击选择单个群组进入群组详细信息主页
	 */
	@RequestMapping(value="/query_group_main_main/{group_id}")
	@ResponseBody
	public ModelAndView queryGroupMainMain(HttpServletRequest request,@Valid @PathVariable  long group_id){
		logger.info("[群组详细信息页面]获取群组详细信息开始...");
		 Map model = new HashMap();
         model.put("group_id", group_id);
         //登录用户
         Long user_id = this.getUserIdInSession(request);
         model.put("user_id", user_id);
         //本群组信息
         List<Map> map = teamGroupService.getGroupByID(group_id);
 		 model.put("data", map.get(0));
 		 model.put("group_id", group_id);
 		 //用户权限查询
 		 String create_id =  map.get(0).get("create_id").toString();
 		 if(user_id.toString().equals(create_id)){
 			model.put("creater", "1");
 		 }
 		 Map<String,Object> params = new HashMap<String,Object>();
 		 params.put("group_id", group_id);
 		params.put("member_id", user_id);
 		Map data = groupMemberService.getGroupMemberById(params);
 		if(data != null && data.get("group_id") != null && ((String)data.get("group_id")).length()>0){
 			model.put("member", "1");
 		}
 		 //共享 按照创建时间排序 取前三个
 		 List<Map> mapshar = groupShareService.getGroupShareListTop3(params);
 		 model.put("dataShar", mapshar);
 		 //话题 按照创建时间排序 取三个
 		 List<Map> maptopic = groupTopicService.getGroupTopicListTop3(params);
		 model.put("dataTopic", maptopic);
// 		 //群动态 按照时间拍戏 取前三个
// 		 List<Map> mapActivity = groupActivityService.getGroupActivityListTop3(params);
//		 model.put("dataActivity", mapActivity);
		 //群动态 按照时间拍戏 取前三个
 		 List<Map> mapDynamic = groupDynamicService.getGroupDynamicListTop3(params);
 		 
 		//更新計數隊列ext
		SumerGroupExtDTO e = new SumerGroupExtDTO();
		e.setGroupId(group_id);
		e.setCnt(1);
		e.setDesc("瀏覽群組");
		e.setType("view_cnt");
		redisGo.putList(SumerGroupExtDTO.DRM_GROUP_EXT_NOTIFY_REDIS_QUEUE_NAME, JsonMapper.toJson(e));
 		 
		 model.put("dataDynamic", mapDynamic);
		 model.put("userId", user_id);
		 model.put("downloadURL", LinkMessages.getString("DOWNLOAD_URL"));
         ModelAndView view = new ModelAndView("teamgroup/group_main_main",model);
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		
		logger.info("[群组详细信息页面]获取群组详细信息结束...");
		
		return view;
	}
	
	
	
	/**
	 * 群组成员
	 * 首页－》家园－》群组－》群组成员
	 */
	
	@RequestMapping(value="/query_group_member/{group_id}")
	@ResponseBody
	public ModelAndView queryGroupMember(HttpServletRequest request,@Valid @PathVariable  long group_id){
		logger.info("[群组成员]获取群组成员开始...");
		Map<String,Object> model = new HashMap<String,Object>();
		//model.put("user", "测试传递变量" );
		Long user_id = this.getUserIdInSession(request);
        Map params = new HashMap();
		params.put("group_id", group_id);
		params.put("member_id", user_id);
		Map creater = groupMemberService.getGroupCreater(params);
		List<GroupMemberVo> data = groupMemberService.getGroupMemberList(params);
		model.put("data", data);
		model.put("creater", creater);
		model.put("userid", user_id);
		model.put("showImgURL", LinkMessages.getString("SHOW_IMG_URL"));
		model.put("userURL", LinkMessages.getString("USER_HOME_URL"));
		//总页数
		ModelAndView view = new ModelAndView("teamgroup/group_member",model);
				
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		//点击tab颜色
		//view.addObject("tabstatus" , "2");
		logger.info("[群组成员]获取群组成员结束...");
		return view;
	}
	
	
	/**
	 * 群组共享
	 * 首页－》家园－》群组－》点击选择单个群组进入群组详细信息－》群组共享
	 */
	
	@RequestMapping(value="/query_group_share/{group_id}/{pageNo}/{pageSize}")
	@ResponseBody
	public ModelAndView queryGroupShare(HttpServletRequest request,@Valid @PathVariable  long group_id,
			@Valid @PathVariable  String pageNo, 
			@Valid @PathVariable String pageSize){
		logger.info("[群组共享]获取群组共享开始...");
		Map<String,Object> model = new HashMap<String,Object>();
		//model.put("user", "测试传递变量" );
		Long userid = this.getUserIdInSession(request);
        Map params = new HashMap();
		params.put("group_id", group_id);
		params.put("member_id", userid);
		if(null==pageSize||"null".equals(pageSize)){
			pageSize = Page.DEFAULT_PAGE_SIZE+"";
		}
		if(null==pageNo||"".equals(pageNo)){
			pageNo = "1";
		}
		params.put("ps", pageSize);
		params.put("pn", pageNo);
		
		Page<GroupShareVo> data = groupShareService.getShareList(params);
		
		int isMember = groupMemberService.isGroupMember(params);
		model.put("data", data);
		//总页数
		model.put("sumPageNum", data.getTotalPageCount());
		model.put("pageSize", Page.DEFAULT_PAGE_SIZE);
		model.put("pageNo",pageNo);
		model.put("userId", userid);
		model.put("isMember", isMember);
		model.put("uploadURL", LinkMessages.getString("UPLOAD_URL"));
		model.put("downloadURL", LinkMessages.getString("DOWNLOAD_URL"));
		
		//总页数
		ModelAndView view = new ModelAndView("teamgroup/group_share",model);
				
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		//点击tab颜色
		//view.addObject("tabstatus" , "2");
		logger.info("[群组共享]获取群组共享结束...");
		return view;
	}
	
	/**
	 * 群组话话题
	 * 首页－》家园－》群组－》群组话题
	 */

	@RequestMapping(value="/query_group_topic/{group_id}/{pageNo}/{pageSize}")
	@ResponseBody
	public ModelAndView queryGroupTopic(HttpServletRequest request,@Valid @PathVariable  long group_id,
			@Valid @PathVariable  String pageNo, 
			@Valid @PathVariable String pageSize,String topicId){
		logger.info("[群组话题]获取群组话题开始...");
		
		Long user_id = this.getUserIdInSession(request);
		 Map model = new HashMap();
         model.put("group_id", group_id);
         
         Map params = new HashMap();
         params.put("group_id", group_id);
         params.put("member_id", user_id);
         if(null==pageSize||"null".equals(pageSize)){
 			pageSize = Page.DEFAULT_PAGE_SIZE+"";
 		}
 		if(null==pageNo||"".equals(pageNo)){
 			pageNo = "1";
 		}
 		params.put("ps", pageSize);
 		params.put("pn", pageNo);
 		
 		Page<GroupTopicVo> data = groupTopicService.getGroupTopicList(params);
 		
 		int isMember = groupMemberService.isGroupMember(params);
 		model.put("data", data);
 		//总页数
 		model.put("sumPageNum", data.getTotalPageCount());
 		model.put("pageSize", Page.DEFAULT_PAGE_SIZE);
 		model.put("pageNo",pageNo);
 		model.put("topicId", topicId);
 		model.put("isMember", isMember);
         
		 ModelAndView view = new ModelAndView("teamgroup/group_topic",model);
		 //每个做后都要加上这个
		 putResourceSrc(view ,request );
			
		 logger.info("[群组话题]获取群组话题结束...");
		return view;
	}
	
	/**
	 * 群组动态 
	 * 首页－》家园－》群组－》点击选择单个群组进入群组详细信息－》主页－》群组动态
	 */
	
	@RequestMapping(value="/query_group_dynamic/{group_id}")
	@ResponseBody
	public ModelAndView queryGroupDynamic(HttpServletRequest request,@Valid @PathVariable  long group_id){
		logger.info("[群组动态]获取群组动态开始...");
		 Map model = new HashMap();
         model.put("group_id", group_id);
         
         Map params = new HashMap();
         params.put("group_id", group_id);
         
         List<Map> data = groupDynamicService.getGroupDynamicList(params);
         model.put("data", data);
         
		 ModelAndView view = new ModelAndView("teamgroup/group_dynamic",model);
		 //每个做后都要加上这个
		 putResourceSrc(view ,request );
		 logger.info("[群组动态]获取群组动态开结束...");
		return view;
	}
	
	/**
	 * 跳转到 新增群组话题页面
	 * @return
	 */
	@RequestMapping(value="/query_homeGroup_topic_add_page/{group_id}")
	@ResponseBody
	public ModelAndView groupTopicAddPage(HttpServletRequest request,@Valid @PathVariable  long group_id){
		Map model = new HashMap();
		 logger.info("[新增群组话题]跳转到新增群组话题开始...");
		model.put("group_id", group_id);
		
		
		ModelAndView view = new ModelAndView("teamgroup/group_topic_add",model);
		Map<String,Object> params = new HashMap<String,Object>();
		
		
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		 logger.info("[新增群组话题]跳转到新增群组话题结束...");
		return view;
	}
	
	/**
	 * 新增群组话题
	 * @return
	 */
	@RequestMapping(value="/query_homeGroup_topic_add/{group_id}/{topic_name}/{topic_content}")
	@ResponseBody
	public ModelAndView groupTopicAdd(HttpServletRequest request
			,@Valid @PathVariable  long group_id
			,@Valid @PathVariable  String topic_name
			,@Valid @PathVariable  String topic_content){
		 logger.info("[新增群组话题]新增群组话题开始...");
		
		Long create_user_id = this.getUserIdInSession(request);
		
		Map model = new HashMap();
		
		
		try {
			
			Map params = new HashMap();
			params.put("group_id", group_id);
			params.put("topic_name", new   String(topic_name.getBytes("ISO8859-1"),"utf-8"));
			params.put("topic_content", new   String(topic_content.getBytes("ISO8859-1"),"utf-8"));
			params.put("create_user_id", create_user_id);
			User user = userService.getUserById(create_user_id);
			params.put("create_user_nm", user.getLoginName());
			groupTopicService.addGroupTopic(params);
			//插入数据
			
			//更新計數隊列ext
			SumerGroupExtDTO e = new SumerGroupExtDTO();
			e.setGroupId(group_id);
			e.setCnt(1);
			e.setDesc("新增話題");
			e.setType("topic_cnt");
			redisGo.putList(SumerGroupExtDTO.DRM_GROUP_EXT_NOTIFY_REDIS_QUEUE_NAME, JsonMapper.toJson(e));
			model.put("result", "成功！");
		} catch (Exception e) {
			// TODO: handle exception
			model.put("result", "error:"+e.getStackTrace());
		}
		
		ModelAndView view = new ModelAndView("teamgroup/result",model);
		
		
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		 logger.info("[新增群组话题]新增群组话题结束...");
		return view;
	}
	/**
	 * 跳转到 新增群组活动页面
	 * @return
	 */
	@RequestMapping(value="/query_homeGroup_activity_add_page/{group_id}")
	@ResponseBody
	public ModelAndView groupActivityAddPage(HttpServletRequest request,@Valid @PathVariable  long group_id){
		 logger.info("[新增群组活动]跳转到新增群组活动开始...");
		System.out.println("aaa");
		Map model = new HashMap();
		model.put("group_id", group_id);
		ModelAndView view = new ModelAndView("teamgroup/group_activity_add",model);
		Map<String,Object> params = new HashMap<String,Object>();
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		logger.info("[新增群组活动]跳转到新增群组活动结束...");
		return view;
	}
	
	/**
	 * 新增群组活动
	 * @return
	 */
	@RequestMapping(value="/query_homeGroup_activity_add",method=RequestMethod.POST)
	@ResponseBody
	public ModelAndView groupActivityAdd(HttpServletRequest request){
		 logger.info("[新增群组活动]新增群组活动开始...");
		Long create_user_id = this.getUserIdInSession(request);
		
		Map model = new HashMap();
		
		long group_id = Long.parseLong(request.getParameter("group_id"));
		String activity_nm = request.getParameter("activity_nm");
		String start_dt = request.getParameter("start_dt");
		String activity_txt = request.getParameter("activity_txt");
		String end_dt = request.getParameter("end_dt");
		
		try {
			
			Map params = new HashMap();
			params.put("group_id", group_id);
			params.put("activity_nm", activity_nm);
			params.put("start_dt", start_dt);
			params.put("end_dt", end_dt);
			params.put("activity_txt",activity_txt);
			params.put("sponsor_id", create_user_id);
			Map data = groupActivityService.getUserById(create_user_id);
			if(data != null){
				params.put("sponsor_nm", data.get("user_lnm"));
			}
	       
			groupActivityService.addGroupActivity(params);
			//插入数据
			
			//更新計數隊列ext
			SumerGroupExtDTO e = new SumerGroupExtDTO();
			e.setGroupId(group_id);
			e.setCnt(1);
			e.setDesc("新增活動");
			e.setType("activity_cnt");
			redisGo.putList(SumerGroupExtDTO.DRM_GROUP_EXT_NOTIFY_REDIS_QUEUE_NAME, JsonMapper.toJson(e));
			
			model.put("result", "成功！");
		} catch (Exception e) {
			e.printStackTrace();
			model.put("result", "error:"+e.getStackTrace());
		}
		
		ModelAndView view = new ModelAndView("teamgroup/result",model);
		
		
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		 logger.info("[新增群组活动]新增群组活动结束...");
		return view;
	}


	

	/**
	 * 跳转到 成员邀请页面
	 * @return
	 */
	@RequestMapping(value="/query_homeGroup_member_add_page/{group_id}")
	@ResponseBody
	public ModelAndView groupMemberAddPage(HttpServletRequest request,@Valid @PathVariable  long group_id){
		 logger.info("[成员邀请]成员邀请开始...");
		Long user_id = this.getUserIdInSession(request);
		
		Map model = new HashMap();
		model.put("group_id", group_id);
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("member_id", user_id+"");
		Map data = groupMemberService.getMemberById(params);
		List friends = new ArrayList();
		if(data!=null && data.get("friends")!=null){
			String friendsStr[] = ((String) data.get("friends")).split(",");
			for (int i = 0; i < friendsStr.length; i++) {
				Map friendMap = new HashMap();
				friendMap.put("friendsID", friendsStr[i]);
				Map fmodel = new HashMap();
				fmodel.put("user_id", friendsStr[i]);
				Map userInfo = groupMemberService.getMemberUserInfo(fmodel);
				friendMap.put("friendsNm",userInfo == null?"佚名":userInfo.get("user_rnm"));
				friends.add(friendMap);
			}
		}
		model.put("friends", friends);
		model.put("group_id", group_id);
		
		model.put("user_id", user_id);
		ModelAndView view = new ModelAndView("teamgroup/group_member_add",model);
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		logger.info("[成员邀请]成员邀请结束...");
		return view;
	}
	
	/**
	 * 邀请成员
	 * sponsor_id:发起者ID     adopt_id:接受者ID
	 * @return
	 */
	@RequestMapping(value="/query_homeGroup_member_add/{group_id}/{sponsor_id}/{adopt_id}/{apply_txt}")
	@ResponseBody
	public ModelAndView groupMemberAdd(HttpServletRequest request
			,@Valid @PathVariable  long group_id
			,@Valid @PathVariable  long sponsor_id
			,@Valid @PathVariable  String adopt_id
			,@Valid @PathVariable  String apply_txt){
		
		logger.info("[成员邀请]成员邀请数据库添加数据开始...");
		
		Long create_user_id = this.getUserIdInSession(request);
		
		Map model = new HashMap();
		
		
		try {
			
			
			
			Map params = new HashMap();
			params.put("group_id", group_id);
			params.put("sponsor_id", sponsor_id);
			
			params.put("apply_txt", new   String(apply_txt.getBytes("ISO8859-1"),"utf-8"));
			
			String[] adopt_id_arr = adopt_id.split(",");
			for(int i=0;i<adopt_id_arr.length;i++){
				params.put("adopt_id", adopt_id);
				teamGroupService.addGroupMember(params);
				
			}
	       
			//groupMemberService.addGroupMember(params);
			
			
			//插入数据
			model.put("result", "成功！");
		} catch (Exception e) {
			e.printStackTrace();
			model.put("result", "error:"+e.getStackTrace());
		}
		
		ModelAndView view = new ModelAndView("teamgroup/result",model);
		
		
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		logger.info("[成员邀请]成员邀请数据库添加数据结束...");
		return view;
	}
	
	/**
	 * 群组主页-编辑公告  保存
	 */
	@RequestMapping(value="/query_homeGroup_member_edit_notice",method=RequestMethod.POST)
	@ResponseBody
	public ModelAndView groupEditNotice(HttpServletRequest request){
		logger.info("[群组公告]编辑公告开始...");
		try {
			long group_id =  Long.parseLong(request.getParameter("group_id"));
		    String notice_text = request.getParameter("txt");
		    
			Map model = new HashMap();
			model.put("group_id", group_id);
			
			Map params = new HashMap();
			params.put("group_id", group_id);
			
			params.put("group_notice",notice_text);
			
			model.put("result", notice_text);
			
			teamGroupService.updateNotice(params);
			
			ModelAndView view = new ModelAndView("teamgroup/result",model);
			
			//每个做后都要加上这个
			putResourceSrc(view ,request );
			logger.info("[群组公告]编辑公告结束...");
			
			//return queryGroupMainMain(request,group_id);
			return view;
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
	}
	
	/**
	 * 主题详情页面
	 * @param request
	 * @param topic_id
	 * @return
	 */
	@RequestMapping(value="/query_homeGroup_topic_detail/{topic_id}")
	@ResponseBody
	public ModelAndView groupTopicDetail(HttpServletRequest request
			,@Valid @PathVariable  long topic_id){
		logger.info("[群组话题]群组话题开始...");
		Map<String,Object> model = new HashMap<String,Object>();
		//model.put("user", "测试传递变量" );
		Long user_id = this.getUserIdInSession(request);
        Map params = new HashMap();
		params.put("topic_id", topic_id);
		Map data = groupTopicService.getGroupTopicByID(params);
		model.put("data", data);
		model.put("userId", user_id);
		model.put("creater", request.getParameter("creater"));
		model.put("member", request.getParameter("member"));
		if(data != null && user_id.equals(data.get("create_id").toString())){
			model.put("topicCreater", "1");
		}
		groupTopicService.updateTopicView(params);
		ModelAndView view = new ModelAndView("teamgroup/group_topic_detail",model);
				
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		//点击tab颜色
		//view.addObject("tabstatus" , "2");
		logger.info("[群组话题]群组话题结束...");
		return view;
		
	}
	
	/**
	 * 活动详情页面
	 * @param request
	 * @param topic_id
	 * @return
	 */
	@RequestMapping(value="/query_homeGroup_activity_detail/{activity_id}")
	@ResponseBody
	public ModelAndView groupActivityDetail(HttpServletRequest request
			,@Valid @PathVariable  long activity_id){
		logger.info("[群组活动]群组活动开始...");
		Map<String,Object> model = new HashMap<String,Object>();
		//model.put("user", "测试传递变量" );
		Long teacherid = this.getUserIdInSession(request);
        Map params = new HashMap();
		params.put("activity_id", activity_id);
		Map data = groupActivityService.getGroupActivityByID(params);
		model.put("data", data);
		List<Map> mem_ids =  groupActivityService.getActivityMemberByID(params);
		model.put("mem_ids", mem_ids);
		model.put("userURL", LinkMessages.getString("USER_HOME_URL"));
		
		ModelAndView view = new ModelAndView("teamgroup/group_activity_detail",model);
				
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		//点击tab颜色
		//view.addObject("tabstatus" , "2");
		logger.info("[群组活动]群组活动结束...");
		return view;
		
	}
	
	
	/**
	 * 删除成员
	 */
	
	@RequestMapping(value="/group_member_del/{member_id}/{group_id}")
	@ResponseBody
	public ModelAndView delGroupMember(HttpServletRequest request,
			@Valid @PathVariable  long member_id,
			@Valid @PathVariable  long group_id){
		logger.info("[群组成员]删除成员开始...");
		Map<String,Object> model = new HashMap<String,Object>();
		try{
			//model.put("user", "测试传递变量" );
			Long teacherid = this.getUserIdInSession(request);
	        Map params = new HashMap();
			params.put("member_id", member_id);
			params.put("del_yn", 1);
			params.put("apply_yn", 1);
			groupMemberService.updateGroupMember(params);
			
			//更新計數隊列ext
			SumerGroupExtDTO e = new SumerGroupExtDTO();
			e.setGroupId(group_id);
			e.setCnt(-1);
			e.setDesc("新增成員");
			e.setType("member_cnt");
			redisGo.putList(SumerGroupExtDTO.DRM_GROUP_EXT_NOTIFY_REDIS_QUEUE_NAME, JsonMapper.toJson(e));
			model.put("result", "成功！");
		} catch (Exception e) {
			// TODO: handle exception
			model.put("result", "error:"+e.getStackTrace());
		}
	
		ModelAndView view = new ModelAndView("teamgroup/result",model);
		putResourceSrc(view ,request );
		logger.info("[群组成员]删除成员结束...");
		return view;
	}
	
	/**
	 * 同意成员
	 */
	
	@RequestMapping(value="/group_member_apply/{member_id}/{group_id}")
	@ResponseBody
	public ModelAndView applyGroupMember(HttpServletRequest request,
			@Valid @PathVariable  long member_id,@Valid @PathVariable  long group_id){
		logger.info("[群组成员]同意成员开始...");
		Map<String,Object> model = new HashMap<String,Object>();
		try{
			Long teacherid = this.getUserIdInSession(request);
	        Map params = new HashMap();
			params.put("member_id", member_id);
			params.put("group_id", group_id);
			params.put("del_yn", 0);
			params.put("apply_yn", 1);
			groupMemberService.updateGroupMember(params);
			
			//更新計數隊列ext
			SumerGroupExtDTO e = new SumerGroupExtDTO();
			e.setGroupId(group_id);
			e.setCnt(1);
			e.setDesc("新增成員");
			e.setType("member_cnt");
			redisGo.putList(SumerGroupExtDTO.DRM_GROUP_EXT_NOTIFY_REDIS_QUEUE_NAME, JsonMapper.toJson(e));
			
			model.put("result", "成功！");
		} catch (Exception e) {
			// TODO: handle exception
			model.put("result", "error:"+e.getStackTrace());
		}
	
		ModelAndView view = new ModelAndView("teamgroup/result",model);
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		//点击tab颜色
		//view.addObject("tabstatus" , "2");
		logger.info("[群组成员]同意成员结束...");
		return view;
	}
	
	/**
	 * 新增共享
	 * @return
	 */
	@RequestMapping(value="/add_group_share/{group_id}/{id}/{oid}/{flength}/{filename}/{code}")
	@ResponseBody
	public ModelAndView groupTopicAdd(HttpServletRequest request
			,@Valid @PathVariable  String group_id
			,@Valid @PathVariable  String id
			,@Valid @PathVariable  String oid
			,@Valid @PathVariable  String flength
			,@Valid @PathVariable  String filename
			,@Valid @PathVariable  String code){
		
		logger.info("[群组共享]新增共享开始...");
		Long user_id = this.getUserIdInSession(request);
		
		Map model = new HashMap();
		
		
		try {
			
			Map params = new HashMap();
			params.put("group_id", group_id);
			//创建资源
			String resource_id_id = UUID.randomUUID().toString();
			params.put("resource_id_id", resource_id_id);
			params.put("topic", "shared resource1");
			params.put("author_id", user_id);
			Map res = groupShareService.addSharedResource(params);
			String share_id = res.get("id")+"";
			//新增文件
			params.put("name", new  String(filename.getBytes("ISO8859-1"),"utf-8"));
			params.put("file_size", flength);
			params.put("file_id", id);
			params.put("resource_id", share_id);
			if(filename.endsWith(".doc") || filename.endsWith(".docx")){
				params.put("file_type", "WORD");
			}else if(filename.endsWith(".pdf")){
				params.put("file_type", "PDF");
			}else if(filename.endsWith(".txt")){
				params.put("file_type", "TEXT");
			}else if(filename.endsWith(".et") || filename.endsWith(".xls") || filename.endsWith(".xlsx")){
				params.put("file_type", "EXCEL");
			}
			groupShareService.addSharedFile(params);
			//增加群组共享
			User user = userService.getUserById(user_id);
			params.put("member_id", user_id);
			params.put("share_id", res.get("id"));
			params.put("member_nm", user.getLoginName());
			groupShareService.addGroupShare(params);
			
			model.put("result", "成功！");
		} catch (Exception e) {
			// TODO: handle exception
			model.put("result", "error:"+e.getStackTrace());
		}
		
		ModelAndView view = new ModelAndView("teamgroup/result",model);
		
		
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		logger.info("[群组共享]新增共享结束...");
		return view;
	}
	
	
	/**
	 * 新增共享
	 * @return
	 */
	@RequestMapping(value="/delete_group_share/{share_id}")
	@ResponseBody
	public ModelAndView groupShareDel(HttpServletRequest request
			,@Valid @PathVariable  String share_id){
		logger.info("[群组共享]删除共享开始...");
		Long user_id = this.getUserIdInSession(request);
		Map model = new HashMap();
		try {
			Map params = new HashMap();
			params.put("share_id", share_id);
			params.put("handle_id", user_id);
			//创建资源
			groupShareService.delGroupShare(params);
			//增加群组共享
			model.put("result", "成功！");
		} catch (Exception e) {
			model.put("result", "error:"+e.getStackTrace());
		}
		ModelAndView view = new ModelAndView("teamgroup/result",model);
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		logger.info("[群组共享]删除共享结束...");
		return view;
	}
	
	/**
	 * 删除活动
	 * @return
	 */
	@RequestMapping(value="/delete_activity/{activity_id}/{group_id}")
	@ResponseBody
	public ModelAndView deleteActivity(HttpServletRequest request,
			@Valid @PathVariable  long activity_id,
			@Valid @PathVariable  long group_id){
		logger.info("[群组活动]删除活动开始...");
		Map model = new HashMap();
		try {
			Map params = new HashMap();
			params.put("activity_id", activity_id);
			groupActivityService.deleteActivity(params);
			
			//更新計數隊列ext
			SumerGroupExtDTO e = new SumerGroupExtDTO();
			e.setGroupId(group_id);
			e.setCnt(-1);
			e.setDesc("刪除活動");
			e.setType("activity_cnt");
			redisGo.putList(SumerGroupExtDTO.DRM_GROUP_EXT_NOTIFY_REDIS_QUEUE_NAME, JsonMapper.toJson(e));
			
			model.put("result", "成功！");
			
		} catch (Exception e) {
			// TODO: handle exception
			
			model.put("result", "error");
		}
		
		ModelAndView view = new ModelAndView("teamgroup/result",model);
		
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		logger.info("[群组活动]删除活动结束...");
		return view;
		
	}

	/**
	 * 编辑话题查询
	 * @return
	 */
	@RequestMapping(value="/group_topic_edit_query/{topic_id}")
	@ResponseBody
	public ModelAndView editTopicQuery(HttpServletRequest request,@Valid @PathVariable  long topic_id){
		logger.info("[群组话题]新增话题开始...");
		Map model = new HashMap();
		try {
			Map params = new HashMap();
			params.put("topic_id", topic_id);
			Map data = groupTopicService.getGroupTopicByID(params);
			model.put("data", data);
			model.put("result", "成功！");
			
		} catch (Exception e) {
			model.put("result", "error");
		}
		ModelAndView view = new ModelAndView("teamgroup/group_topic_edit",model);
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		logger.info("[群组话题]新增话题结束...");
		return view;
		
	}
	
	/**
	 * 编辑话题
	 * @return
	 */
	@RequestMapping(value="/group_topic_edit",method=RequestMethod.POST)
	@ResponseBody
	public ModelAndView editTopic(HttpServletRequest request){
		logger.info("[群组话题]编辑话题开始...");
		Map model = new HashMap();
		try {
			Map params = new HashMap();
			params.put("topic_id", request.getParameter("topic_id"));
			params.put("topic_nm",request.getParameter("topic_nm"));
			params.put("topic_txt",request.getParameter("topic_content"));
			groupTopicService.updateTopic(params);
			model.put("result", "成功！");
			
		} catch (Exception e) {
			// TODO: handle exception
			
			model.put("result", "error");
		}
		
		ModelAndView view = new ModelAndView("teamgroup/result",model);
		
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		logger.info("[群组话题]编辑话题结束...");
		return view;
		
	}
	
	
	/**
	 * 编辑活动查询
	 * @return
	 */
	@RequestMapping(value="/group_activity_edit_query/{activity_id}")
	@ResponseBody
	public ModelAndView editActivityQuery(HttpServletRequest request,@Valid @PathVariable  long activity_id){
		logger.info("[群组活动]活动查询开始...");
		Map model = new HashMap();
		try {
			Map params = new HashMap();
			params.put("activity_id", activity_id);
			Map data = groupActivityService.getGroupActivityByID(params);
			model.put("data", data);
			model.put("group_id", data.get("group_id"));
			model.put("result", "成功！");
		} catch (Exception e) {
			model.put("result", "error");
		}
		ModelAndView view = new ModelAndView("teamgroup/group_activity_edit",model);
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		logger.info("[群组活动]活动查询结束...");
		return view;
		
	}
	
	/**
	 * 编辑活动
	 * @return
	 */
	@RequestMapping(value="/group_activity_edit",method=RequestMethod.POST)
	@ResponseBody
	public ModelAndView editActivity(HttpServletRequest request){
		logger.info("[群组活动]编辑活动开始...");
		Map model = new HashMap();
		try {
			Map params = new HashMap();
			params.put("activity_id", request.getParameter("activity_id"));
			params.put("start_dt", request.getParameter("start_dt"));
			params.put("end_dt", request.getParameter("end_dt"));
			params.put("activity_nm", request.getParameter("activity_nm"));
			params.put("activity_txt",request.getParameter("activity_txt"));
			groupActivityService.updateGroupActivity(params);
			model.put("result", "成功！");
			
		} catch (Exception e) {
			// TODO: handle exception
			
			model.put("result", "error");
		}
		
		ModelAndView view = new ModelAndView("teamgroup/result",model);
		
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		logger.info("[群组活动]编辑活动结束...");
		return view;
		
	}
	
	
	
	/**
	 * 上传头像页面
	 * @return
	 */
	@RequestMapping(value="/group_main_upimg",method=RequestMethod.POST)
	@ResponseBody
	public ModelAndView groupUpimg(HttpServletRequest request){
		logger.info("[群组头像]跳转到上传头像页面开始...");
		Map model = new HashMap();
		
		String group_id = request.getParameter("group_id");
		String group_img = request.getParameter("group_img");
		model.put("group_id", group_id);
		if("default".equals(group_img)){
			group_img = null;
		}
		model.put("group_img", group_img);
		model.put("uploadImgURL", LinkMessages.getString("UPLOADIMG_URL"));
		model.put("showImgURL", LinkMessages.getString("SHOW_IMG_URL"));
		ModelAndView view = new ModelAndView("teamgroup/group_main_upimg",model);
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		logger.info("[群组头像]跳转到上传头像页面结束...");
		return view;
		
	}
	
	/**
	 * 上传头像
	 * @return
	 */
	@RequestMapping(value="/update_group_img",method=RequestMethod.POST)
	@ResponseBody
	public ModelAndView updateGroupImg(HttpServletRequest request){
		logger.info("[群组头像]上传头像开始...");
		long group_id = Long.parseLong(request.getParameter("group_id"));
		String group_img = request.getParameter("group_img");
		
		Map model = new HashMap();
		model.put("group_id", group_id);
		model.put("group_img", group_img);
		teamGroupService.updateGroupImg(model);
		ModelAndView view = new ModelAndView("teamgroup/result",model);
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		logger.info("[群组头像]上传头像结束...");
		return view;
		
	}
	
	/**
	 * 报名参加活动
	 * 
	 */
	@RequestMapping(value="/add_members_activity/{activity_id}")
	@ResponseBody
	public ModelAndView add_members_activity(HttpServletRequest request
			,@Valid @PathVariable  long activity_id){
		
		logger.info("[群组活动]报名参加活动开始...");
		Long user_id = this.getUserIdInSession(request);
		
		Map model = new HashMap();
		Map params = new HashMap();
		
		params.put("activity_id", activity_id);
		params.put("member_id", user_id);
		
		try {
			groupActivityService.addMembersActicity(params);
			
			model.put("result", "成功！");
			
		} catch (Exception e) {
			// TODO: handle exception
			model.put("result", "error");
		}
		
		ModelAndView view = new ModelAndView("teamgroup/result",model);
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		logger.info("[群组活动]报名参加活动结束...");
		return view;
		
	}
	
	/**
	 * 下载
	 * 
	 */
	@RequestMapping(value="/down_group_share/{share_id}")
	@ResponseBody
	public ModelAndView downGroupShare(HttpServletRequest request
			,@Valid @PathVariable  long share_id){
		logger.info("[群组共享]下载开始...");
		Map model = new HashMap();
		Map params = new HashMap();
		params.put("share_id", share_id);
		params.put("down_cnt", 1);
		
		try {
			groupShareService.updateSharedResource(params);
			model.put("result", "成功！");
		} catch (Exception e) {
			// TODO: handle exception
			model.put("result", "error");
		}
		ModelAndView view = new ModelAndView("teamgroup/result",model);
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		logger.info("[群组共享]下载结束...");
		return view;
		
	}
	
	/**
	 * 个人群组管理
	 * 
	 */
	@RequestMapping(value="/self_group_manage",params={"pageNo","pageSize"},method = {RequestMethod.GET})
	@ResponseBody
	public ModelAndView selfGroupManage(HttpServletRequest request,
			String pageNo, 
			String pageSize){
		logger.info("[个人群组管理]管理页面加载数据开始...");
		Map model = new HashMap();
		Map params = new HashMap();
		
		Long member_id = this.getUserIdInSession(request);
		
		params.put("member_id", member_id);
		
		if(null==pageSize||"".equals(pageSize)){
 			pageSize = Page.DEFAULT_PAGE_SIZE+"";
 		}
 		if(null==pageNo||"".equals(pageNo)){
 			pageNo = "1";
 		}
 		params.put("ps", pageSize);
 		params.put("pn", pageNo);
		
		List<TeamGroupVo> data = teamGroupService.selfGroupManage(params);
		model.put("data", data);
		//总页数
		model.put("pageSize", Page.DEFAULT_PAGE_SIZE);
		model.put("pageNo",pageNo);
		model.put("member_id", member_id);
		
		ModelAndView view = new ModelAndView("teamgroup/group_pt",model);
		//每个做后都要加上这个
		putResourceSrc(view ,request );
		logger.info("[个人群组管理]管理页面加载数据结束...");
		return view;
		
	}
	
 
	
	@RequestMapping(value="/getHeadImg/{userId}/{comment_id}")
	@ResponseBody
	public Map downGroupShare(HttpServletRequest request 	,@Valid @PathVariable  Long userId,@PathVariable String comment_id){
		logger.info("[群组共享]获取头像...");
		 
		Map params = new HashMap();
		Map<String,Object> result = new HashMap<String,Object>();		
		if(!StringUtils.equals(comment_id, "null"))
			result.put("comment_id", comment_id);
		if(userId == 0)
			userId = this.getUserIdInSession(request);
		
		User user = userService.getUserById( ( userId   ) );
		
		result.put("himg", user!=null?user.getUser_himg():"");
		result.put("uid", userId);
		result.put("uname", user.getUserName() );
		return result;
		
	}
	
}
